package com.xm.xmapp;

import java.io.File;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.CustomLayout;
import com.xm.xmapp.customview.PhoneDialog;
import com.xm.xmapp.dialog.AppCommentTipDialog;
import com.xm.xmapp.dialog.TipLeftRightDialog;
import com.xm.xmapp.http.UpdataAppClass;
import com.xm.xmapp.http.UpdataAppClass.HandlerInterFace;
import com.xm.xmapp.myinterface.DialogInterFace;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.VersionRequest;
import com.xm.xmapp.response.bean.ResponseVersion;
import com.xm.xmapp.tools.Tools;

public class AboutOurSelfActivity extends BaseActivity implements OnClickListener {

	private TextView title, tv_text;
	/**
	 * 版本检测更新
	 */
	ResponseVersion bean;
	ResponseVersion.T t;
	private ImageView back;
	private CustomLayout cl_check_app, cl_connect_us, cl_comment;
	private TipLeftRightDialog mydialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_aboutourself);

	}

	@Override
	public void initView() {

		title = (TextView) findViewById(R.id.title);
		tv_text = (TextView) findViewById(R.id.tv_text);
		back = (ImageView) findViewById(R.id.back);

		cl_check_app = (CustomLayout) findViewById(R.id.cl_check_app);
		cl_connect_us = (CustomLayout) findViewById(R.id.cl_connect_us);
		cl_comment = (CustomLayout) findViewById(R.id.cl_comment);
	}

	@Override
	public void initData() {

		title.setText("关于我们");
		cl_check_app.setText2(Tools.getVersion());
		tv_text.setText("V" + Tools.getVersion());
		cl_connect_us.setText2(CodeConstant.phone);
		cl_connect_us.setImg2DrawableLeft(R.mipmap.guanyu_dianhua_icon);
	}

	@Override
	public void initEvent() {

		back.setOnClickListener(this);
		cl_check_app.setOnClickListener(this);
		cl_connect_us.setOnClickListener(this);
		cl_comment.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {

		switch (arg0.getId()) {
		case R.id.back:
			finish();
			break;

		case R.id.cl_check_app:

			promptDialog.show();
			VersionRequest requestBean = new VersionRequest();
			requestBean.app_version_number = Tools.getVersionCode();
			requestBean.phone_model = Tools.getMODEL();
			MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

				@Override
				public void onResponse(String response) {

					promptDialog.dismiss();
					bean = JSON.parseObject(response, ResponseVersion.class);
					if (bean.is_version_update == 1) {
						t = JSON.parseObject(bean.version_info, ResponseVersion.T.class);
						UpdataAppClass.APK_NAME = "yibuyongche_" + t.app_show_version_number;
						File file = new File(MyAppcation.getMyAppcation().getExternalFilesDir("myapk") + "/",
								UpdataAppClass.APK_NAME + ".apk");
						if (!file.exists()) {
							if (t.is_force_update == 1) {
								showBuilder("有新版本", "文件大小:" + t.app_file_size + "\n" + t.app_version_description, 1);
							} else {
								showBuilder("有新版本", "文件大小:" + t.app_file_size + "\n" + t.app_version_description, 1);
							}
						} else {
							UpdataAppClass.getUpdataAppClass().update();
						}
					} else {
						Tools.showMessage("已是最新版本");
					}

				}
			}, new DisssmissInterFace() {

				@Override
				public void putError(VolleyError error) {
					promptDialog.dismiss();
				}
			});

			break;

		case R.id.cl_connect_us:
			PhoneDialog phoneDialog = new PhoneDialog(this);
			phoneDialog.show();
			break;

		case R.id.cl_comment:
			new AppCommentTipDialog(this).show();
			break;

		default:
			break;
		}
	}

	protected void showBuilder(String title, String message, int flag) {

		mydialog = new TipLeftRightDialog(this, title, message, flag);
		mydialog.setCancelable(true);
		mydialog.setCanceledOnTouchOutside(false);
		mydialog.setDialogInterFace("取消", "下载", new DialogInterFace() {

			@Override
			public void right(int flag, Object data) {

				UpdataAppClass.getUpdataAppClass().downFile(t.url, AboutOurSelfActivity.this, new HandlerInterFace() {

					@Override
					public void run(Message msg) {
					}
				});
				mydialog.dismiss();
			}

			@Override
			public void left(int flag, Object data) {
				mydialog.dismiss();
			}
		}).show();

	}
}
